from io import BytesIO

import xlwt
from django.contrib.admin.utils import quote
from django.http import JsonResponse, HttpRequest, HttpResponse
from django.shortcuts import render, redirect
from polls.models import Subject, Teacher, User
from polls.utils import genRandomCode, Captcha


def show_subjects(request):
    subjects = Subject.objects.all().order_by('no')
    return render(request, 'subjects.html', {'subjects': subjects})


def show_teachers(request):
    try:
        sno = int(request.GET.get('sno'))
        teachers = []
        if sno:
            km = Subject.objects.only('name').get(no=sno)
            teachers = Teacher.objects.filter(subject=km).order_by('no')
        return render(request, 'teachers.html',
                      {
                          'subject': km,
                          'teachers': teachers

                      })
    except (ValueError, Subject.DoesNotExist):
        return redirect("/")


def goodOrBad(request):
    if request.session.get('userid'):
        try:
            tno = int(request.GET.get('tno'))
            teacher = Teacher.objects.get(no=tno)
            if request.path.startswith("/good"):
                teacher.good_count += 1
                count = teacher.good_count
            else:
                teacher.bad_count += 1
                count = teacher.bad_count
            teacher.save()
            data = {
                'code': 200,
                'msg': '操作成功',
                'count': count,

            }
        except (ValueError, Teacher.DoesNotExist):
            data = {
                'code': 201,
                'msg': '点评失败',
                'count': count,

            }
    else:
        data = {'code': 202, 'msg': '请先登录'}

    return JsonResponse(data)


def dologin(request: HttpRequest) -> HttpResponse:
    hint = ''
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        yzm = request.POST.get('captcha')
        captcha = request.session['captcha'].lower()
        # captcha=captcha.lower()
        if yzm == captcha:
            if username and password:
                # password = gen_md5_digest(password)
                user = User.objects.filter(username=username, password=password).first()
                if user:
                    request.session['userid'] = user.no
                    request.session['username'] = user.username
                    return redirect('/')
                else:
                    hint = '用户名或密码错误'
            else:
                hint = '请输入有效的用户名和密码'
        else:
            hint = '验证码错误'
    return render(request, 'login.html', {'hint': hint})


def logout(request):
    """注销"""
    request.session.flush()
    return redirect('/')


# Create your views here.
def get_captcha(request: HttpRequest) -> HttpResponse:
    """验证码"""
    captcha_text = genRandomCode()
    request.session['captcha'] = captcha_text
    image_data = Captcha.instance().generate(captcha_text)
    return HttpResponse(image_data, content_type='image/png')


def hi(request):
    return render(request, "hi.html", {'hi': '徐睿'})


def login(request):
    return render(request, "login.html")

def export_teachers_excel(request):
    # 创建工作簿
    wb = xlwt.Workbook()
    # 添加工作表
    sheet = wb.add_sheet('老师信息表')
    # 查询所有老师的信息
    queryset = Teacher.objects.all().select_related('subject')
    # 向Excel表单中写入表头
    colnames = ('姓名', '介绍', '好评数', '差评数', '学科')
    for index, name in enumerate(colnames):
        sheet.write(0, index, name)
    # 向单元格中写入老师的数据
    props = ('name', 'intro', 'good_count', 'bad_count', 'subject')
    for row, teacher in enumerate(queryset):
        for col, prop in enumerate(props):
            value = getattr(teacher, prop, '')
            if isinstance(value, Subject):
                value = value.name
            sheet.write(row + 1, col, value)
    # 保存Excel
    buffer = BytesIO()
    wb.save(buffer)
    # 将二进制数据写入响应的消息体中并设置MIME类型
    resp = HttpResponse(buffer.getvalue(), content_type='application/vnd.ms-excel')
    # 中文文件名需要处理成百分号编码
    filename = quote('教师.xls')
    # 通过响应头告知浏览器下载该文件以及对应的文件名
    resp['content-disposition'] = f'attachment; filename*=utf-8''{filename}'
    return resp

def get_teachers_data(request):
    queryset = Teacher.objects.all().only('name','good_count','bad_count')
    names = [teacher.name for teacher in queryset]
    good_counts = [teacher.good_count for teacher in queryset]
    bad_counts = [teacher.bad_count for teacher in queryset]
    return JsonResponse({'names': names, 'good': good_counts, 'bad': bad_counts})


def report(request):

    return render(request, 'good.html')